package cn.diancan.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;

import cn.diancan.config.Config;

public class FileHandle {
	public FileHandle(){
		
	}
	public static File createNewFile(String fileNP) throws IOException{//根据字符串生成一个空文件（非目录）
		File file=new File(fileNP);
		if(!file.exists())
			file.mkdirs();  
		if(!file.isFile()){
			file.delete();
			file.createNewFile();
		}
		return file; 
	}
	/**
	 * 根据数据，生成一个文件路径
	 * @param fileID
	 * @return
	 */
	public static String parsePath(long fileID){
		int ID = (int)fileID;
        if(ID <= 0 || ID > 0x7fffffff)
            return "";
        int number = 1;
        Hashtable<String, String> left = new Hashtable<String, String>();
        int i;
        for(i = 0; number > 0; i++)
        {
            left.put(String.valueOf(i), String.valueOf(ID % 100));
            number = ID / 100;
            ID = number;
        } 
        i--;
        String folder = "";
        for(; i >=0; i--)
        {
            String temp = (String)left.get(String.valueOf(i));
            if(Integer.parseInt(temp) < 0 && Integer.parseInt(temp) >= 100)
                return "";
            folder =folder+File.separator+temp;
        }
        return folder;
}
	public static void copyFile(String oldPath,String newPath) throws Exception{        //文件复制
        File oldFile=createNewFile(oldPath);
        File newFile=createNewFile(newPath);
        copyFile(oldFile,newFile);
    }
	public static void copyBigFile(String oldPath,String newPath,int method) throws Exception{ //大文件复制
        File inFile=createNewFile(oldPath);
        File outFile=createNewFile(newPath);
        if(method==1){
        	fileByteCopy(inFile,outFile);
        }else if(method==2){
        	fileBufferCopy(inFile,outFile);
        }else if(method==3){
        	byteMapFileCopy(inFile,outFile);
        }
    }
	public static void fileByteCopy(File inFile,File outFile) throws Exception{     //1传统IO包得复制方法
        FileInputStream  in =new FileInputStream(inFile); 
        FileOutputStream out=new FileOutputStream(outFile,true);
        byte[] bytes=new byte[1024]; 
        int i; 
        while((i=in.read(bytes))!=-1){ 
            out.write(bytes,0,i); 
        } 
        in.close(); 
        out.close(); 
    } 
	public static void fileBufferCopy(File inFile,File outFile) throws Exception {//2新IO包中基于channel和direct buffer
		FileChannel inChannel=new FileInputStream(inFile).getChannel(); 
        FileChannel ouChannel=new FileOutputStream(outFile).getChannel(); 
        ByteBuffer buffer=ByteBuffer.allocateDirect(1024); 
        @SuppressWarnings("unused")
		int i=0; 
        while(true){ 
            buffer.clear(); 
            if((i=inChannel.read(buffer))==-1) 
            break; 
            buffer.flip(); 
            ouChannel.write(buffer); 
        } 
        inChannel.close(); 
        ouChannel.close();
    } 
	public static void byteMapFileCopy(File inFile,File outFile) throws Exception { //3 直接影射，MappedByteBuffer
	        FileChannel out=new FileOutputStream(outFile).getChannel(); 
	        MappedByteBuffer buffer=new FileInputStream(inFile).getChannel().map(FileChannel.MapMode.READ_ONLY,0,inFile.length()); 
	        buffer.load(); 
	        out.write(buffer); 
	        buffer=null; 
	        out.close(); 
	}
	
	public static void copyFile(File oldFile,File newFile) throws Exception{             //文件复制 
	    FileInputStream in=new FileInputStream(oldFile);
	    copyFile(in, newFile);
	}
	
	public static void copyFile(InputStream in,File newFile)throws Exception{ 
	        if(!newFile.exists())
	        	newFile.createNewFile(); 
	        FileOutputStream out=new FileOutputStream(newFile);
	        int c; 
	        byte buffer[]=new byte[1024];
	        while((c=in.read(buffer))!=-1){
	            for(int i=0;i<c;i++)
	                out.write(buffer[i]);        
	        }
	        in.close(); 
	        out.flush();
	        out.close();
	}
	
	public static void writeFile(String filePath,String content) throws Exception{     //根据内容写入文件 
		File file=createNewFile(filePath);
		writeFile(file,content); 
    }
	public static void writeFile(File file,String content) throws Exception{           //根据内容写入文件
		FileOutputStream  out=new FileOutputStream(file);   
		out.write(content.getBytes());  
		out.close();  
    }
	public static StringBuilder getReadLine(File fileIn) throws Exception{             //根据文件 得到内容
			StringBuilder sb = new StringBuilder(""); 
			FileReader fr = new FileReader(fileIn) ;
	        BufferedReader br = new BufferedReader(fr) ;
	        String sLine = br.readLine() ;
	        while (sLine!=null){
	        		sb.append(sLine);
	                sLine = br.readLine() ;
	        }
	        fr.close() ;
			return sb;
    }
	public static List<String> getReadLineList(File fileIn) throws Exception{             //根据文件 得到内容
		List<String> sb = new ArrayList<String>(); 
		FileReader fr = new FileReader(fileIn) ;
        BufferedReader br = new BufferedReader(fr) ;
        String sLine = br.readLine() ;
        while (sLine!=null){
        		sb.add(sLine);
                sLine = br.readLine() ;
        }
        fr.close() ;
		return sb;
}
	public static StringBuilder getReadLine(String fileIn) throws Exception{              //根据文件 得到内容
		File file= createNewFile(fileIn);
		return getReadLine(file);
    }
	public static List<String> getReadLineList(String fileIn) throws Exception{          //根据文件 得到内容
		File file= createNewFile(fileIn);
		return getReadLineList(file);
	}
	
	
	
}
